<template>
  <div class="result-container">
    <div class="title-wrap">
      <h2 class="title">{{$route.query.q}}</h2>
      <span class="sub-title">找到{{count}}个结果</span>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲" name="songs">
        <table class="el-table">
          <thead>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr class="el-table__row" v-for="(item,index) in songlist" :key="index" @dblclick="playMusic(item.id)">
              <td>{{index+1}}</td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <!-- 歌曲名 -->
                    <span>{{item.name}}</span>
                    <!-- mv标签 -->
                    <span v-if="item.mvid !=0" class="iconfont icon-mv"></span>
                  </div>
                  <!-- 二级革命 -->
                  <!-- <span v-if="item.alias.length!=0">{{item.alias[0]}}</span> -->
                </div>
              </td>
              <!-- 歌手名 -->
              <td>{{item.artists[0].name}}</td>
              <td>{{item.album.name}}</td>
              <td>{{item.duration}}</td>
            </tr>

          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane label="歌单" name="lists">
        <div class="items">
          <div class="item" v-for="(item,index) in playlists" :key="index" @click="toPlayList(item.id)">
            <div class="img-wrap">
              <div class="num-wrap">
                播放量:
                <span class="num">{{item.playCount}}</span>
              </div>
              <img :src="item.coverImgUrl" alt="" />
              <span class="iconfont icon-play"></span>
            </div>
            <p class="name">{{item.name}}</p>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="MV" name="mv">
        <div class="items mv">
          <div class="item" v-for="(item,index) in mv" :key="index" @click="toMv(item.id)">
            <div class="img-wrap">
              <!-- 封面 -->
              <img :src="item.cover" alt="" />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <!-- 播放总数 -->
                <div class="num">{{item.playCount}}</div>
              </div>
              <!-- 时长 -->
              <span class="time">{{item.duration}}</span>
            </div>
            <div class="info-wrap">
              <!-- mv名称 -->
              <div class="name">{{item.name}}</div>
              <!-- 歌手名 -->
              <div class="singer">{{item.artistName}}</div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'result',
  data() {
    return {
      activeIndex: 'songs',
      //保存歌曲
      songlist:[],
      //歌单
      playlists:[],
      // 保存mv
      mv:[],
      //搜索结果
      count:0
    };
  },
  created() {
    axios({
      url:'https://autumnfish.cn/search',
      method:'get',
      params:{
        keywords:this.$route.query.q,
        type:1,
        limit:15
      }
    }).then(res=>{
      this.songlist=res.data.result.songs;
      this.count=res.data.result.songCount;
      
      // this.reload()
      //计算歌曲时间
      for(let i=0;i<this.songlist.length;i++){
        let min = parseInt(this.songlist[i].duration/1000/60)
        let sec = parseInt(this.songlist[i].duration/1000%60)
        if(min<10){
          min='0'+ min
        }
        if(sec<10){
          sec='0'+ sec
        }
        this.songlist[i].duration=min+":"+sec
      }

    })
  },
  watch:{
    activeIndex(){

      let type=1;

      let limit=10
      switch (this.activeIndex) {
        case 'songs':
          type=1;
          limit=20
          break;
        case 'lists':
          type=1000;
          limit=20
          break;
        case 'mv':
          type=1004;
          limit=12
          break;
        default:
          break;
      }
      axios({
      url:'https://autumnfish.cn/search',
      method:'get',
      params:{
        keywords:this.$route.query.q,
        type,
        limit
      }
    }).then(res=>{
      console.log(res);
      if(type==1){

      this.songlist=res.data.result.songs;
      this.count=res.data.result.songCount;
      //计算歌曲时间
      for(let i=0;i<this.songlist.length;i++){
        let min = parseInt(this.songlist[i].duration/1000/60)
        let sec = parseInt(this.songlist[i].duration/1000%60)
        if(min<10){
          min='0'+ min
        }
        if(sec<10){
          sec='0'+ sec
        }
        this.songlist[i].duration=min+":"+sec
      }
      }else if(type==1000){
        //歌单
        this.playlists=res.data.result.playlists
        //总数
        this.count=res.data.result.playlistCount
        // 处理播放次数
        for (let i = 0; i < this.playlists.length; i++) {
          if(this.playlists[i].playCount>100000){
            this.playlists[i].playCount=parseInt(this.playlists[i].playCount/10000) + '万'

          }
        }
      }else{
        this.mv=res.data.result.mvs
        //总数
        this.count=res.data.result.mvCount
        // 处理数据
        for (let i = 0; i <this.mv.length; i++) {
          let min =parseInt(this.mv[i].duration/1000/60)
          let sec=parseInt(this.mv[i].duration/1000%60)
          if(min<10){
            min = '0' + min
          }
          if(sec<10){
            sec = '0' + sec
          }
          this.mv[i].duration=min+":"+sec

          //播放次数
          if(this.mv[i].playCount>100000){
            this.mv[i].playCount=parseInt(this.mv[i].playCount/10000)+'万'

          }
        }
      }

    })

    }
  },
  // 播放
  methods: {
    playMusic(id){
      axios({
        url:'https://autumnfish.cn/song/url',
        // https://autumnfish.cn/top/artists
        method:'get',
        params:{
          id
        }
      }).then(res=>{
        let url = res.data.data[0].url
        this.$parent.musicUrl = url
      })
    },

    //歌单跳转
    toPlayList(id){
      //跳转并携带数据
      this.$router.push(`/playlist?q=${id}`)
    },

    // 去mv
    toMv(id){
      this.$router.push(`/mv?q=${id}`)
    }
  },
  
}
</script>

<style >

</style>
